<script setup>
import Vrouter from "@/router"
import { onMounted, ref, nextTick } from 'vue'
import { history_search } from '@/api/search/search.js'
import {MYURL as URL} from '@/utils/express/express.js'
const getback = () => {
    const router = Vrouter;
    router.push('myrecpmmend')
}
const input = ref(null)
onMounted(() => {
    input.value.focus()
})
let my_history = ref(JSON.parse(localStorage.getItem('history')))
let has_history = ref(false)
let has_history_than_ten = ref(false)
if (my_history.value != null) {
    has_history.value = true
}
onMounted(() => {
    try {
        if (my_history.value.length >= 8) {
            has_history_than_ten.value = true
            my_history.value.splice(7)
        }
    }
    catch (err) {
        console.log('欢迎来到搜索');
    }
})

let search_hot_list = ref(null)
const bilibili_hot = ref(null)
history_search().then((res) => {
    if(res.data.status) return
    search_hot_list.value = res.data.message
    for (let i = 0; i < search_hot_list.value.length; i++) {
        if (search_hot_list.value[i].hot_tag === 'hot') {
            nextTick(() => {
                bilibili_hot.value.children[i].innerHTML += `<img style="width: 19px;" src="${URL}/public/icon/hot.png">`
            })
        }
        if (search_hot_list.value[i].hot_tag === 'geng') {
            nextTick(() => {
                bilibili_hot.value.children[i].innerHTML += `<img style="width: 19px;" src="${URL}/public/icon/geng.png">`
            })
        }
        if (search_hot_list.value[i].hot_tag === 'new') {
            nextTick(() => {
                bilibili_hot.value.children[i].innerHTML += `<img style="width: 19px;" src="${URL}/public/icon/new.png">`
            })
        }
    }
})
const search_btn = () => {
    const router = Vrouter
    let data
    input.value.value === '' ? data = input.value.placeholder : data = input.value.value
    router.push({
        path: 'search_video',
        query: { search: data }
    })
}
const search_nav_btn = (e) => {
    const router = Vrouter
    input.value.value = e
    router.push({
        path: 'search_video',
        query: { search: input.value.value }
    })
}
const clear_history = () => {
    has_history_than_ten.value = false
    has_history.value = false
    sure_delete.value = false
    localStorage.removeItem("history")
    my_history.value = []
}
const triangle = ref(null)
let triangle_state = true
const show_all = () => {
    if (triangle_state) {
        triangle.value.style.transform = ` rotate(180deg)`
        triangle_state = false
        my_history.value = JSON.parse(localStorage.getItem('history'))
    } else {
        my_history.value.splice(7)
        triangle.value.style.transform = ` rotate(0deg)`
        triangle_state = true
    }
}
const sure_delete = ref(false) 
</script>

<template>
    <div v-show="sure_delete" class="sure-delete">
        <div>
            <p>删除全部搜索历史?</p>
            <div>
                <p @click="sure_delete = false ">取消</p>
                <p @click="clear_history">全部删除</p>
            </div>
        </div>
    </div>
    <div class="search-nav">
        <img class="back-image" src="@/assets/img/getback.png" @click="getback">
        <div class="top-nav-search">
            <img src="@/assets/img/search.png">
            <input @keydown.enter="search_btn" ref="input" type="text" placeholder="uniapp">
        </div>
        <p @click="search_btn">搜索</p>
    </div>
    <div class="sarch-hot-nav">
        <p>bilibili热搜</p>
        <!-- <div class="sarch-hot-nav-all">
            <p>完整榜单</p>
            <p>></p>
        </div> -->
    </div>
    <div class="bilibili-hot" ref="bilibili_hot">
        <div class="bilibili_list_main" v-for="val in search_hot_list" @click="search_nav_btn(val.hot_name)" :key="val.id">
            <p>{{ val.hot_name }}</p>
        </div>
    </div>
    <div class="search-histroy">
        <div class="search-histroy-top">
            <p>搜索历史</p>
            <div class="search-histroy-right" v-if="has_history">
                <img @click="sure_delete = true" src="@/assets/img/rubish.png">
                <p v-if="has_history_than_ten">|</p>
                <img v-if="has_history_than_ten" ref="triangle" src="@/assets/img/downrow.png" @click="show_all">
            </div>
        </div>
        <div class="search-histroy-main">
            <p v-for="item in my_history" @click="search_nav_btn(item)" :key="item">{{ item }}</p>
        </div>
    </div>
</template>

<style scoped lang="less">
.sure-delete {
    top: 0;
    left: 0;
    position: fixed;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.4);
 
    >div {
        border-radius: 10px;
        width: 70vw;
        height: 15vh;
        background-color: white;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        display: flex;
        flex-direction: column;
        justify-content: center;
        gap: 15%;
        >p {
            text-align: center;
        font-weight: 600;
    }
    >div {
        border-top: 1px solid rgba(0, 0, 0, 0.06);
        padding-top: 10px;
            display: flex;
            justify-content: space-around;
            p {
                font-weight: 600;
            }
            p:nth-child(2) {
                color: var(--bilibili-pink);
            }
        }
    }
}
.hot {
    font-size: 40px;
}

input::-webkit-input-placeholder {
    font-size: 14px;
}

.search-nav {
    margin-top: 10px;
    display: flex;
    align-items: center;

    p {
        color: #ed709a;
        margin-left: 2%;
    }
}

.back-image {
    width: 10px;
    margin-left: 5%;
}

.top-nav-search {
    margin-left: 3%;
    display: flex;
    align-items: center;
    width: 70%;
    height: 29px;
    border: 1px #95999f solid;
    border-radius: 20px;

    input {
        margin-left: 5px;
        width: 90%;
        outline: none;
        border: none;
        font-size: 15px;
    }

    img {
        margin-left: 2%;
        width: 15px;
        object-fit: contain;
    }

    p {
        font-size: 12px;
        margin-left: 2%;
        color: #6b6f75;
    }
}

.sarch-hot-nav {
    margin-top: 20px;
    padding: 0px 10px;
    display: flex;
    justify-content: space-between;
    font-size: 14px;
}

.sarch-hot-nav p:nth-child(1) {
    color: #1f1f1f;
    font-weight: 600;
}

.sarch-hot-nav-all {
    display: flex;
    align-items: center;
    gap: 3px;
}

.sarch-hot-nav-all p:nth-child(1) {
    color: #999999;

}

.sarch-hot-nav-all p:nth-child(2) {
    color: #999999;
    font-size: 14px;
}

.bilibili-hot {
    font-size: 15px;
    padding: 10px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px 0px;

    p {
        max-width: 40vw;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }
}

.bilibili_list_main {
    width: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: center;
}

.search-histroy {
    margin-top: 20px;
    padding: 0 10px;
}

.search-histroy-top {
    display: flex;
    align-content: flex-start;

    p {
        font-size: 14px;
        color: #1f1f1f;
        font-weight: 600;
    }

    .search-histroy-right {
        margin-left: auto;
        display: flex;
        gap: 10px;
        align-items: center;

        img {
            width: 15px;
            height: 18px;
        }

        p {
            font-size: 12px;
            color: #d2d2d2;
        }
    }
}

.search-histroy-main {
    margin-top: 15px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;

    p {
        max-width: 30vw;
        font-size: 14px;
        padding: 7px 12px;
        border-radius: 5px;
        background-color: #f6f7f9;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }
}
</style>
